package com.fox.imagedb.gui;

/*
 * Copyright (c) 1995 - 2008 Sun Microsystems, Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Sun Microsystems nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

//package components;
/*
 * This code is based on an example provided by Richard Stanford, 
 * a tutorial reader.
 */

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeListener;
import java.io.File;

import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;

import com.fox.imagedb.action.NextImageAction;
import com.fox.imagedb.action.PreviousImageAction;
import com.fox.imagedb.listener.MainPanelDividerListener;
import com.fox.imagedb.model.FileExplorer;

public class MainPanel extends JSplitPane implements ActionListener {

	private static final long serialVersionUID = -519830113768494493L;

	private static final String NEXT_IMAGE = "next_image";

	private static final Object PREVIOUS_IMAGE = "previous_image";

	// private JLabel image;

	private TopPanel topPanel;

	private FileExplorer fileExplorer;

	private JTextPane propertiesPanel;

	public MainPanel() {

		super(JSplitPane.VERTICAL_SPLIT);
		setDividerLocation(0.9);
		setResizeWeight(0.5);

		// Create the components.
		fileExplorer = new FileExplorer();
		this.topPanel = new TopPanel();

		initializeKeyBindings();

		PropertyChangeListener pcl = new MainPanelDividerListener(this);
		this.addPropertyChangeListener(pcl);
		topPanel.addPropertyChangeListener(pcl);

		setTopComponent(topPanel);
		setBottomComponent(new JPanel());

	}

	public void actionPerformed(ActionEvent e) {
		System.out.println(e);
	}

	public DataPanel getDataPanel() {
		return topPanel.getDataPanel();
	}

	public FileExplorer getFileExplorer() {
		return fileExplorer;
	}

	public ImagePanel getImagePanel() {
		return topPanel.getImagePanel();
	}

	public JTextPane getProperties() {
		return propertiesPanel;
	}

	private void initializeKeyBindings() {
		ActionMap actionMap = this.getActionMap();
		InputMap inputMap = this.getInputMap(JSplitPane.WHEN_IN_FOCUSED_WINDOW);

		inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), NEXT_IMAGE);
		inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0),
				PREVIOUS_IMAGE);

		inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0), NEXT_IMAGE);
		inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), PREVIOUS_IMAGE);

		actionMap.put(NEXT_IMAGE, new NextImageAction(this));
		actionMap.put(PREVIOUS_IMAGE, new PreviousImageAction(this));
	}

	public void setFileExplorer(FileExplorer fileExplorer) {
		this.fileExplorer = fileExplorer;
	}

	public void updateImage(File image) {
		topPanel.updateImage(image);
	}

}
